package com.jxl.demo.control;

import com.jxl.demo.pojo.Comment;
import com.jxl.demo.pojo.Forum;
import com.jxl.demo.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.Normalizer;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")


public class ForumController {
    @Autowired(required = false)
    private JdbcTemplate jdbc;


    //新增帖子
    //http://192.168.137.139:8080/newForum
    @PostMapping("/newForum")
    public Result newForum(@RequestBody Forum forum){
        Result result = new Result();
        try {
            jdbc.update("insert into forum(stuNumber, note, imgAddress, likes) value (?,?,?,0)",
                    forum.getStuNumber(), forum.getNote(),forum.getImgAddress());
            result.setCode(200);
            result.setResult("发帖子成功");
            System.out.println("发帖子成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //展示帖子
    //http://192.168.137.139:8080/showForum
    @GetMapping("/showForum")
    public Result showForum(){
        Result result = new Result();
        try {
            List<Forum> forum = jdbc.query("select * from forum; ",
                    new BeanPropertyRowMapper<>(Forum.class));
            result.setCode(200);
            result.setResult(forum);
            System.out.println("展示帖子成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //点赞帖子
    //http://192.168.137.139:8080/likeForum
    @PostMapping("/likeForum")
    public Result likeForum(@RequestBody Forum forum){
        Result result = new Result();
        try {
            System.out.println(forum);
            jdbc.update("update forum set likes = likes + 1 where id = ?", forum.getId());
            result.setCode(200);
            result.setResult("点赞帖子成功");
            System.out.println("点赞帖子成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //发布评论
    //http://192.168.137.139:8080/newComment
    @PostMapping("/newComment")
    public Result newComment(@RequestBody Comment comment){
        Result result = new Result();
        try {
            jdbc.update("insert into comment(forumId, stuNumber, content) values (?, ?, ?)",
                    comment.getForumId(), comment.getStuNumber(), comment.getContent());
            result.setCode(200);
            result.setResult("发布评论成功");
            System.out.println("发布评论成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //获取帖子的所有评论
    //http://192.168.137.139:8080/getComments
    @PostMapping("/getComments")
    public Result getComments(@RequestBody Comment comment){
        Result result = new Result();
        try {
            List<Comment> comments = jdbc.query(
                    "select * from comment where forumId = ?",
                    new BeanPropertyRowMapper<>(Comment.class), comment.getForumId()
            );
            result.setCode(200);
            result.setResult(comments);
            System.out.println("获取帖子评论成功");
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

}
